﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AVCare.LMS.Models;
using AVCare.LMS.DataAccess;

namespace AVCare.LMS.LocalData
{
    public class UserHelper : IUserHelper
    {
        LMSEntities entities = new LMSEntities();

        public List<Role> GetUserRole(string userID)
        {

            var query = from r in entities.Roles
                        where
                        (from rid in entities.UserRoles
                         where rid.UserID == userID
                         select rid.RoleID).Contains(r.ID)
                        select r;



            return query.ToList();
        }

        public void Save(User user)
        {
            entities.Users.Add(user);
            entities.SaveChanges();
        }

        public User GetUser(string userID)
        {

            var query = entities.Users.FirstOrDefault(u => u.ID == userID);
            if (query == null)
            {
                return null;
            }
            User user = new User { ID = query.ID, Name = query.Name, Password = query.Password };
            return user;

        }
    }
}
